﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ExcellOnServices.Models;
using ExcellOnServices.Models.Utils;

namespace ExcellOnServices.Areas.Admin.Controllers
{
    public class ServicesController : Controller
    {
        private ExcellOnServicesContext db = new ExcellOnServicesContext();

        // GET: /Admin/Services/
        [Authorize]
        public ActionResult Index(string search, bool orderPriceAsce = false, bool redirectAfterCreate = false)
        {
            var services = db.Services.OrderByDescending(s => s.CreatedDateTime).Select(s => s);

            if (!String.IsNullOrEmpty(search))
            {
                services = services.Where(s => s.Name.ToUpper().Contains(search.ToUpper()));
            }

            if (!redirectAfterCreate)
            {
                if (orderPriceAsce)
                {
                    services = services.OrderBy(s => s.Price);
                }
                else
                {
                    services = services.OrderByDescending(s => s.Price);
                }
            }
            ViewBag.Search = search;
            ViewBag.OrderPriceAsce = orderPriceAsce;
            return View(services.ToList());
        }

        // GET: /Admin/Services/Details/5
        [Authorize]
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Service service = db.Services.Find(id);
            if (service == null)
            {
                return HttpNotFound();
            }
            return View(service);
        }

        // GET: /Admin/Services/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Admin/Services/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult Create([Bind(Include = "Name,Description,Price,ImageUrl")] Service service, HttpPostedFileBase ImageUrl)
        {
            if (ModelState.IsValid)
            {
                if (ImageUrl != null && ImageUrl.ContentLength > 0)
                {
                    string fileName = UniqueFileNames.GetUniqueFileName(System.IO.Path.GetFileName(ImageUrl.FileName));

                    service.ImageUrl = "/Resources/services/" + fileName;

                    ImageUrl.SaveAs(Server.MapPath("~/") + service.ImageUrl);
                }

                db.Services.Add(service);
                db.SaveChanges();
                return RedirectToAction("Index", new { redirectAfterCreate = true });
            }

            return View(service);
        }

        // GET: /Admin/Services/Edit/5
        [Authorize]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Service service = db.Services.Find(id);
            if (service == null)
            {
                return HttpNotFound();
            }
            return View(service);
        }

        // POST: /Admin/Services/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult Edit([Bind(Include = "Id,Name,Description,Price,ImageUrl")] Service service, HttpPostedFileBase ImageUrl)
        {
            if (ModelState.IsValid)
            {
                if (ImageUrl != null && ImageUrl.ContentLength > 0)
                {
                    string fileName = UniqueFileNames.GetUniqueFileName(System.IO.Path.GetFileName(ImageUrl.FileName));

                    service.ImageUrl = "/Resources/img/" + fileName;

                    ImageUrl.SaveAs(Server.MapPath("~/") + service.ImageUrl);
                }

                db.Entry(service).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(service);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
